package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.dal.Dal_Xwfarmroom;
import com.xwdeshj.entity.XwFarmRoominfo;
import com.xwdeshj.repository.XwFarmRoominfoRepository;
import com.xwdeshj.service.XwFarmRoominfoService;
import com.xwdeshj.utils.helper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by BKWJA on 2019/1/6.
 * 农家乐——农家住宿信息 实现类
 */
@Slf4j
@Service("xwFarmRoominfoService")
public class XwFarmRoominfoServiceImpl implements XwFarmRoominfoService {

    @Autowired
    private XwFarmRoominfoRepository xwFarmRoominfoRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Sid sid;

    Dal_Xwfarmroom dal_xwfarmroom = new Dal_Xwfarmroom();

    /**
     * 获取农家乐店铺 住宿信息
     */
    @Override
    public JSONObject getfarmroomlist(Integer pageindex, Integer pagesize, String starttime, String endtime,
                                      String sortfild, String sortway,String searchval,String searchkey,
                                      String farm_encryptId,HttpServletRequest request) {
        JSONObject result = new JSONObject();

        result = dal_xwfarmroom.getfarmroomlist(pageindex,pagesize,starttime,endtime,sortfild,sortway,
                searchval,searchkey,farm_encryptId,jdbcTemplate);

        System.out.println("getfarmroomlist:"+result.toString());
        return result;
    }


    /**
     * 新建 修改农家乐住宿信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject updatefarmroom(Integer room_id, String roomType, String remarks, String roomprice,
                                     String roomnum, String farm_encryptId, String business_encryptId,
                                     HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){

            if(room_id == null || room_id == 0){
                //新建
                XwFarmRoominfo xwFarmRoominfo = new XwFarmRoominfo();
                xwFarmRoominfo.setFarmEncryptid(farm_encryptId);
                xwFarmRoominfo.setBusinessEncryptid(business_encryptId);
                xwFarmRoominfo.setFarmRoomType(roomType);
                xwFarmRoominfo.setFarmRoomDesc(remarks);
                xwFarmRoominfo.setFarmRoomPrice(Double.parseDouble(roomprice));
                xwFarmRoominfo.setFarmRoomNumber(Integer.parseInt(roomnum));
                xwFarmRoominfo.setFarmRoomCreatetime(new Date());
                xwFarmRoominfo.setFarmRoomUpdatetime(new Date());
                try {
                    xwFarmRoominfoRepository.save(xwFarmRoominfo);
                    result.put("status", 0);
                    result.put("msg", "ok");

                }catch (Exception e){
                    e.printStackTrace();
                    result.put("status",40002);
                    result.put("msg",e);
                }

            }else {
                //修改
                XwFarmRoominfo xwFarmRoominfo = new XwFarmRoominfo();
                xwFarmRoominfo.setFarmRoomId(room_id);
                boolean tmep = xwFarmRoominfoRepository.exists(Example.of(xwFarmRoominfo));
                if(tmep){
                    XwFarmRoominfo farmRoominfo = xwFarmRoominfoRepository.findByFarmRoomId(room_id);
                    farmRoominfo.setFarmRoomType(roomType);
                    farmRoominfo.setFarmRoomDesc(remarks);
                    farmRoominfo.setFarmRoomPrice(Double.parseDouble(roomprice));
                    farmRoominfo.setFarmRoomNumber(Integer.parseInt(roomnum));
                    farmRoominfo.setFarmRoomUpdatetime(new Date());
                    try {
                        xwFarmRoominfoRepository.save(farmRoominfo);
                        result.put("status", 0);
                        result.put("msg", "ok");

                    }catch (Exception e){
                        e.printStackTrace();
                        result.put("status",40002);
                        result.put("msg",e);
                    }
                }
            }

        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("updatefarmroom:"+result.toString());
        return result;
    }

    /**
     *   根据住宿id，删除对应的信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject delfarmroom(String room_id, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){
            if(!room_id.equals("")){
                if(room_id.indexOf(",") == -1){
                    try {
                        //单个删除
                        int flag = xwFarmRoominfoRepository.deleteByFarmRoomId(Integer.parseInt(room_id));
                        if(flag == 1){
                            result.put("status",0);
                            result.put("msg","ok");
                        }else {
                            result.put("status",40002);
                            result.put("msg","删除失败！");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        result.put("status",40002);
                        result.put("msg",e);
                    }
                }else {
                    //批量删除
                    int flag = 0;
                    String[] tids = room_id.split(",");
                    for (int i = 0; i < tids.length; i++) {
                        flag = xwFarmRoominfoRepository.deleteByFarmRoomId(Integer.parseInt(tids[i]));
                    }
                    if(flag == 1){
                        result.put("status",0);
                        result.put("msg","ok");
                    }else {
                        result.put("status",40002);
                        result.put("msg","接口出现错误！");
                    }
                }
            }
        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("delfarmroom:"+result.toString());
        return result;
    }


    public Dal_Xwfarmroom getDal_xwfarmroom() {
        return dal_xwfarmroom;
    }

    public void setDal_xwfarmroom(Dal_Xwfarmroom dal_xwfarmroom) {
        this.dal_xwfarmroom = dal_xwfarmroom;
    }
}
